package com.hancai.barrage.service.impl;

import com.hancai.barrage.mapper.ClientMapper;
import com.hancai.barrage.model.Page;
import com.hancai.barrage.pagehelper.QueryObject;
import com.hancai.barrage.pojo.Client;
import com.hancai.barrage.service.ClientService;
import com.hancai.barrage.utils.Constant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 涵菜
 * @version 1.0
 */
@Service
public class ClientServiceImpl implements ClientService {


    private final ClientMapper clientMapper;

    public ClientServiceImpl(ClientMapper clientMapper) {
        this.clientMapper = clientMapper;
    }
    @Override
    public Page<Client> query(QueryObject qo) {
        Long total=clientMapper.selectForCount(qo);
        if(total==0){
            return Page.empty(qo.getPageNum(),qo.getPageSize());
        }
        List<Client>list=clientMapper.selectForList(qo);
        return new Page<>(qo.getPageNum(),qo.getPageSize(),total,list);
    }

    @Override
    public void save(Client obj) {
        //给客户设置默认密码,当客户密码不为空时
        if(!StringUtils.hasText(obj.getPassword())){
            obj.setPassword(Constant.DEFAULT_CLIENT_PASSWORD);
        }
        if(!StringUtils.hasText(obj.getAvatar())){
            obj.setAvatar(Constant.DEFAULT_CLIENT_AVATAR);
        }
        obj.setStatus(Client.STATUS_NORMAL);
        obj.setDeleted(Client.NO_DELETED);
        clientMapper.insert(obj);
    }

    @Override
    public void updateById(Client obj) {
        clientMapper.updateByPrimaryKey(obj);
    }

    @Override
    public void deleteById(Long id) {
        clientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Client selectById(Long id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Client> selectAll() {
        return clientMapper.selectAll();
    }

    @Override
    public void changeStatus(Long id, Integer status) {
        clientMapper.changeStatus(id,status);
    }

    @Override
    public void logicDeleteById(Long id, boolean deleted) {
        clientMapper.logicDeleteById(id,deleted);
    }
}
